package gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import messagesPacks.SubExtendBorrowMessage;
import messagesPacks.SubscriberBorrowDataMessage;

import org.omg.CORBA.INITIALIZE;

import Entity.Book;
import Entity.BorrowRecored;

import controllers.IntSysClock;
import controllers.OBLSettings;
import controllers.ServerUIP;
/**
 * This class representing a Subscriber Extend Borrow Gui
 * Date: 10/1/2013
 * @author Roman
 *
 */

public class SubscriberExtendBorrowUI extends JPanel 
{
	 private static final long serialVersionUID = 1L;
	 JSeparator separator;
	 private SubscriberUI subUI = null ;
	 private JLabel lblTitleLabel;

	 JButton btnLogOut;
	 JButton btnExit;
	 JButton btnBack;
	 JButton btnExtend;
	 
	 int selected_row;
	 public SimpleTable currentBorrowsTable ;
	 int cannotExtendFlag = 0;
	 BorrowRecored  br_rec = null ; 
	 
	 SubscriberBorrowDataMessage QueryAnswer = null ;  
	 
	 
	public SubscriberExtendBorrowUI(SubscriberUI fatherPanel) 
	{
		super();
		initialize();
		System.out.println("SubscriberSearchBookUI created.");
		this.subUI = fatherPanel;
	}

	private void initialize()
	{
		
	    this.setSize(OBLSettings.pane2Size[0],OBLSettings.pane2Size[1]);
		setLayout(null);
		
		
		separator = new JSeparator();
		separator.setBounds(-26, 57, 826, 24);
		add(separator);
		
		lblTitleLabel = new JLabel("OBL Extend borrows");
		lblTitleLabel.setBounds(276, -1, 247, 60);
		lblTitleLabel.setFont(new Font("Tahoma", Font.BOLD, 20));
		add(lblTitleLabel);
		
		btnLogOut = new JButton("Logout");
		btnLogOut.setBounds(614, 11, 83, 41);
		btnLogOut.setFont(new Font("Tahoma", Font.BOLD, 15));
		add(btnLogOut);
		
		btnExit = new JButton("Exit");
		btnExit.setBounds(707, 11, 83, 41);
		btnExit.setFont(new Font("Tahoma", Font.BOLD, 15));
		add(btnExit);
		
		btnBack = new JButton("Back");
		btnBack.setBounds(26, 11, 83, 41);
		btnBack.setFont(new Font("Tahoma", Font.BOLD, 15));
		add(btnBack);
		
		Object ColNames[] = { "Borrow Number","CopyID","ISBN", "Book Name","Borrow Date","Return Date","Orders for this book" };
		currentBorrowsTable = new SimpleTable(null, ColNames, 10, 70, 780, 250);
		currentBorrowsTable.setColCenter(currentBorrowsTable.table.getColumnCount());
	    add(currentBorrowsTable.tblScrollPane,BorderLayout.CENTER);
		
		btnExtend = new JButton("Extend");
		btnExtend.setFont(new Font("Tahoma", Font.PLAIN, 14));
		btnExtend.setBounds(351, 485, 97, 41);
		btnExtend.setEnabled(false);
		add(btnExtend);
		
		this.br_rec = new BorrowRecored();
//================================================================		
//            Action Listeners
//================================================================		
		btnBack.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				 currentBorrowsTable.model.setRowCount(0);
				 btnExtend.setEnabled(false);
                 WelcomeUI.wui.switchPanel("OBL System - Subscriber",subUI ,OBLSettings.pane2Size[0], OBLSettings.pane2Size[1]);
			}
		});
//================================================================		
		currentBorrowsTable.table.getSelectionModel().addListSelectionListener(new ListSelectionListener() 
		{
			public void valueChanged(ListSelectionEvent e) 
			{
				JTable table = currentBorrowsTable.table;

					 if(table.getRowCount() > 0)	
					 {	
						selected_row = currentBorrowsTable.table.getSelectedRow();
	
						btnExtend.setEnabled(true);
						if(selected_row >= 0)
						{ 
				    		Object[] rowData = new Object[table.getColumnCount()];  
				    		for (int i = 0; i < table.getColumnCount(); i++) 
				    		{  
						         rowData[i] = table.getValueAt(selected_row, i);  
				    		} 
	
				    		br_rec.borrowNumber = Integer.parseInt(rowData[0].toString());
				    		br_rec.copyID   = rowData[1].toString();
				    		br_rec.bookISBN = rowData[2].toString();
				    		br_rec.bookName = rowData[3].toString();
				    		br_rec.borrowDate = rowData[4].toString();
				    		br_rec.returnDate = rowData[5].toString();
				    		br_rec.ordersForBook = rowData[6].toString();
						  }
				     }
			}
		});	
//================================================================		
		btnExtend.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
				if(cannotExtendFlag == 0 )  
				{  SubExtendBorrowMessage msg_res = subUI.subscriberUIP.extendSubscriberBorrow(br_rec);
	
				  if(msg_res.msg_result == 2)
						JOptionPane.showMessageDialog( WelcomeUI.wui, "There are orders for this book " ,"Information",JOptionPane.INFORMATION_MESSAGE);
				  else    
				  if(msg_res.msg_result == 0)
					{		
					   JOptionPane.showMessageDialog( WelcomeUI.wui, "this book is overdue ,/n " +
					   								"original return date was on "+msg_res.Br.returnDate+", " +
					   								"please return this book to the library" ,"Warning",JOptionPane.WARNING_MESSAGE);
					}
				  else    
				  if(msg_res.msg_result == 1)
					{		
					   	JOptionPane.showMessageDialog( WelcomeUI.wui, "you have more then a week to return date " ,"Warning",JOptionPane.WARNING_MESSAGE);	
					}
				  else 
				  if(msg_res.msg_result == -1)
				   {
					 JOptionPane.showMessageDialog( WelcomeUI.wui, "there was an error in extend process , borrow period not extended " ,"Warning",JOptionPane.WARNING_MESSAGE);
				   }
				 else
				  if(msg_res.msg_result == 3)
				   {  
					JOptionPane.showMessageDialog(WelcomeUI.wui,"return date extended for a week , until : "+ msg_res.newReturnDate, "Information",JOptionPane.INFORMATION_MESSAGE);
					currentBorrowsTable.table.setValueAt(msg_res.newReturnDate, selected_row, 5);
				   }
				}
				else
				   JOptionPane.showMessageDialog( WelcomeUI.wui, "you have an overdue borrow , you cannot extend any book " ,"Warning",JOptionPane.WARNING_MESSAGE);
		   }
	  });
	
  }//init

//=============================================================================================	
		public void showCurrentBorrows(String userID) //calling this function from subscriberUI -> btnViewHistory.addActionListener(new ActionListener() {...
		{
			QueryAnswer = subUI.subscriberUIP.showBorrows(userID);
			   
			if(QueryAnswer.msg_result == -1)
				JOptionPane.showMessageDialog( WelcomeUI.wui, "there was an error in retrieving your borrow data " ,"Warning",JOptionPane.WARNING_MESSAGE);
			
			else if(QueryAnswer.msg_result == 0)
				{
				    JOptionPane.showMessageDialog( WelcomeUI.wui, "you don't have any active borrows " ,"Information",JOptionPane.INFORMATION_MESSAGE);
				}
			else if(QueryAnswer.msg_result == 1)
				{
					cannotExtendFlag = 0;
					for(BorrowRecored var : QueryAnswer.borrowList)
					{
					   currentBorrowsTable.model.addRow(new Object[] {var.borrowNumber,var.copyID,var.bookISBN,
							   var.bookName,var.borrowDate,var.returnDate,var.ordersForBook});
					}
				}
			else 
			{	
				JOptionPane.showMessageDialog( WelcomeUI.wui, "you have overdue in borrows" ,"Warning",JOptionPane.WARNING_MESSAGE);
				cannotExtendFlag = 1;
				
				for(BorrowRecored var : QueryAnswer.borrowList)
				{
				   currentBorrowsTable.model.addRow(new Object[] {var.borrowNumber,var.copyID,var.bookISBN,
						   var.bookName,var.borrowDate,var.returnDate,var.ordersForBook});
				}
			 }
		}
		public void paintComponent(Graphics g) 
		{
			g.drawImage((OBLSettings.IMG_DEFAULT_BACKGROUND).getImage(), 0, 0, OBLSettings.pane2Size[0], OBLSettings.pane2Size[1],null);
			
		}
	
}//class
